import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3
import QtQuick.Dialogs 1.3
import com.RspInterface 1.0
import "public.js" as Public


//风险客户
Item {
    id: riskCust
    property string listName: "RiskCust"
    property var tabName
    property var branchID
    property bool bChangeWidth: false
    property bool bInital: false
    property string activeCustID: ""
    signal freshData
    onBranchIDChanged: freshData()

    ColumnLayout {
        anchors.fill: parent
        spacing: 10
        RowLayout {
            anchors.left: parent.left
            anchors.leftMargin: 10
            anchors.top: parent.top
            anchors.topMargin: 10
            anchors.right: parent.right
            anchors.rightMargin: 20
            Layout.preferredHeight: 40
            spacing: 5

            Text {
                text: qsTr("显示") + branchID + qsTr("前")
            }
            ComboBox {
                id: topNum
                Layout.preferredWidth: 55
                editable: true
                validator: RegExpValidator {
                    regExp: /\d{1,6}/
                }
                model: [10, 20, 30, 50]
            }
            Text {
                text: qsTr("位")
            }
            ComboBox {
                id: belongType
                Layout.preferredWidth: 65
                model: ListModel {
                    ListElement {
                        text: "间接"
                        belong: 1
                    }
                    ListElement {
                        text: "直接"
                        belong: 0
                    }
                }
            }
            Text {
                text: qsTr("客户")
            }

            Text {
                text: qsTr("账户类型：")
            }

            ComboBox {
                id: acctType
                Layout.preferredWidth: 65
                model: acctTypeModel
            }
            Item {
                Layout.fillWidth: true
            }
            Button {
                id: query
                text: "查询"
                onClicked: {
                    var top = parseInt(topNum.editText)
                    console.log("查询人数" + top)
                    if (!Public.isRealNum(topNum.editText)) {
                        tipInput.text = "请输入正确的查询人数"
                        tipInput.open()
                    } else {
                        //                        getCondition()
                        freshData()
                    }
                }
            }

            //            Button {
            //                text: "关闭"
            //            }
            Item {
                Layout.preferredWidth: 25 //fillWidth把最后一个挤出去的bug补救
            }
        }

        TabView {
            id: frame
            Layout.fillHeight: true
            anchors.left: parent.left
            anchors.leftMargin: 5
            anchors.right: parent.right
            anchors.rightMargin: 5
            //            frameVisible: true
            signal listWidthChange(string role, int width)
            //            anchors.topMargin: 10
            Tab {
                title: "绝对强平"
                ListFrame {
                    id: listForceAbs
                    tablelistName: listName
                    Component.onCompleted: {
                        listForceAbs.tableList.model = g_MainQuo.getRiskCustModel(
                                    "绝对强平", getCondition() + "#risk_grade=3")
                    }
                    onRightClick: {
                        console.log("onRightClick [" + row + "," + col + "]")
                        //                        listForceAbs.tableList.model.copyCellText(row, col)
                        rBtnMenu.row = row
                        rBtnMenu.col = col
                        rBtnMenu.model = listForceAbs.tableList.model
                        rBtnMenu.popup()
                    }
                    onGridWidthChange: {
                        if (bInital && col >= 0)
                            frame.listWidthChange(
                                        listForceAbs.tableList.getColumn(
                                            col).role,
                                        listForceAbs.tableList.getColumn(
                                            col).width)
                    }
                    Connections {
                        //对其它Tab也有效，神奇
                        target: colSet
                        onUpdateUIColumn: {
                            //                            console.log("Connections " + sList + " " + listName)//不会打印
                            if (sList == listName) {
                                bInital = false //lqh 更新表头会导致改变列宽，触发保存列宽，没有必要
                                listForceAbs.updateColumn(listName)
                                bInital = true
                            }
                        }
                    }

                    Connections {
                        target: riskCust
                        onFreshData: {
                            listForceAbs.tableList.model = g_MainQuo.getRiskCustModel(
                                        "绝对强平",
                                        getCondition() + "#risk_grade=3")
                            //                            listForce.tableList.model = g_MainQuo.getRiskCustModel(
                            //                                        "相对强平",
                            //                                        getCondition() + "#risk_grade=4")
                            //                            listCall.tableList.model = g_MainQuo.getRiskCustModel(
                            //                                        "追保", getCondition() + "#risk_grade=1")
                            //                            listNormal.tableList.model = g_MainQuo.getRiskCustModel(
                            //                                        "正常", getCondition() + "#risk_grade=6")
                        }
                    }

                    onLeftClick: {
                        forceSingle.enabled = true
                        activeCustID = tableList.model.getCellText(
                                    tableList.currentRow, 0)
                    }

                    onDoubleClick: openSingleForce("手工单客户强平", activeCustID)
                    onLoadHeaderFinish: bInital = true
                }
            }
            Tab {
                title: "相对强平"
                ListFrame {
                    id: listForce
                    tablelistName: listName
                    Component.onCompleted: {
                        listForce.tableList.model = g_MainQuo.getRiskCustModel(
                                    "相对强平", getCondition() + "#risk_grade=4")
                    }
                    onRightClick: {
                        rBtnMenu.row = row
                        rBtnMenu.col = col
                        rBtnMenu.model = listForce.tableList.model
                        rBtnMenu.popup()
                    }
                    onLeftClick: {
                        forceSingle.enabled = true
                        activeCustID = tableList.model.getCellText(
                                    tableList.currentRow, 0)
                    }
                    onDoubleClick: openSingleForce("手工单客户强平", activeCustID)
                    Connections {
                        target: riskCust
                        onFreshData: {
                            listForce.tableList.model = g_MainQuo.getRiskCustModel(
                                        "相对强平",
                                        getCondition() + "#risk_grade=4")
                        }
                    }
                }
            }
            Tab {
                title: "追保"
                ListFrame {
                    id: listCall
                    tablelistName: listName
                    Component.onCompleted: {
                        listCall.tableList.model = g_MainQuo.getRiskCustModel(
                                    "追保", getCondition() + "#risk_grade=1")
                    }
                    onRightClick: {
                        rBtnMenu.row = row
                        rBtnMenu.col = col
                        rBtnMenu.model = listCall.tableList.model
                        rBtnMenu.popup()
                    }
                    onLeftClick: {
                        forceSingle.enabled = false
                        activeCustID = tableList.model.getCellText(
                                    tableList.currentRow, 0)
                    }

                    Connections {
                        target: riskCust
                        onFreshData: {
                            listCall.tableList.model = g_MainQuo.getRiskCustModel(
                                        "追保", getCondition() + "#risk_grade=1")
                        }
                    }
                }
            }

            Tab {
                title: "正常"
                ListFrame {
                    id: listNormal
                    tablelistName: listName //"RiskCust2"
                    Component.onCompleted: {
                        tableList.model = g_MainQuo.getRiskCustModel(
                                    "正常", getCondition() + "#risk_grade=6")
                    }
                    onRightClick: {
                        rBtnMenu.row = row
                        rBtnMenu.col = col
                        rBtnMenu.model = listNormal.tableList.model
                        rBtnMenu.popup()
                    }
                    onLeftClick: {
                        forceSingle.enabled = false
                        activeCustID = tableList.model.getCellText(
                                    tableList.currentRow, 0)
                    }

                    Connections {
                        target: riskCust
                        onFreshData: {
                            listNormal.tableList.model = g_MainQuo.getRiskCustModel(
                                        "正常", getCondition() + "#risk_grade=6")
                        }
                    }
                }
            }

            onListWidthChange: {
                if (!bInital)
                    return
                console.log("Tabview onListWidthChange " + role + " " + width)
                colCfg.setColumnWidth(listName, role, width)
                bChangeWidth = true
                for (var i = 0; i < frame.count; ++i) {
                    //                    console.log(i + " Tabview " + frame.getTab(i).item)
                    var tab = frame.getTab(i).item
                    if (i != frame.currentIndex && tab !== null)
                        tab.setColumnWidth(role, width)
                }
            }

            onCurrentIndexChanged: {
                console.log("Tabview onActiveFocusChanged " + currentIndex)
                var tab = frame.getTab(currentIndex).item
                if (currentIndex == 0 || currentIndex == 1) {

                    console.log("Tabview tableview cur " + tab.tableList.currentRow)
                    if (tab.tableList.currentRow >= 0) {
                        forceSingle.enabled = true
                        activeCustID = tab.tableList.model.getCellText(
                                    tab.tableList.currentRow, 0)
                    } else
                        activeCustID = ""
                } else {
                    forceSingle.enabled = false
                    if (tab.tableList.currentRow >= 0) {
                        activeCustID = tab.tableList.model.getCellText(
                                    tab.tableList.currentRow, 0)
                    } else
                        activeCustID = ""
                }

                console.log("activeCustID:" + activeCustID)
            }
        }

        RowLayout {
            Layout.preferredHeight: 40
            spacing: 20
            Button {
                text: "设置列"
                anchors.left: parent.left
                anchors.leftMargin: 25
                onClicked: {
                    colSet.visible = true
                    //                    colSet.refList = frame
                    colSet.loadColumnCfg(listName)
                }
            }
            Item {
                Layout.preferredWidth: parent.width / 4
            }
            Button {
                id: forceSingle
                text: "手工强平"
                enabled: false
                //                anchors.left: parent.left
                //                anchors.leftMargin: parent.width / 3
                onClicked: {
                    var tab = frame.getTab(frame.currentIndex).item
                    console.log("forceSingle " + tab.tableList.model)
                    openSingleForce("手工单客户强平", activeCustID)
                }
            }
            Button {
                text: "批量强平"
                onClicked: {
                    console.log("click 批量强平" + branchID)
                    openBatchForce("批量强平", branchID)
                }
            }
            Button {
                text: "客户信息"
                enabled: activeCustID == "" ? false : true
                onClicked: {
                    custInfo.visible = true
                    custInfo.acctNo = activeCustID
                    console.log("客户信息 ")
                }
            }
            Item {
                Layout.fillWidth: true
            }
            //            Button {
            //                text: "关闭"
            //                anchors.right: parent.right
            //                anchors.rightMargin: 25
            //                onClicked: closeTab(tabName)
            //            }
            //            Item {
            //                Layout.preferredWidth: 25 //fillWidth把最后一个挤出去的bug补救
            //            }
        }
    }
    Button {
        text: "关闭"
        anchors.right: parent.right
        anchors.rightMargin: 25
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 10
        onClicked: closeTab(tabName)
    }
    function getCondition() {
        var sCond = "#branch_id=" + branchID
        sCond += "#top_number=" + topNum.editText
        sCond += "#belong=" + belongType.model.get(
                    belongType.currentIndex).belong
        console.log("getCondition " + sCond)
        return sCond
    }

    Timer {
        id: timer_save_column
        interval: 50000
        repeat: true
        triggeredOnStart: true
        running: true
        onTriggered: {
            if (!bChangeWidth)
                return
            bChangeWidth = false
            colCfg.saveColumnWidth()
            console.log("save_column ")
        }
    }

    //    Timer {
    //        id: timer_Ini
    //        interval: 3000
    //        repeat: false
    //        triggeredOnStart: true
    //        running: true
    //        onTriggered: {
    //            bInital = true
    //        }
    //    }
    Dialog {
        id: custInfo
        title: "客户信息"
        width: 760
        height: 400
        property var acctNo: null
        property RspInterface custDetail2

        //: g_MainQuo.getCustDetail(acctNo)
        contentItem: Rectangle {

            //            color: "blue"
            ColumnLayout {
                anchors.fill: parent
                CustDetail {
                    id: custDe
                    anchors.top: parent.top
                    anchors.left: parent.left
                    //                    anchors.leftMargin: -30
                    anchors.right: parent.right
                    anchors.rightMargin: 150 //CustDetail内容向右偏移的补救
                    Layout.preferredHeight: parent.height / 2
                }

                StorePosi {
                    anchors.bottom: parent.bottom
                    anchors.left: parent.left
                    anchors.right: parent.right
                    Layout.preferredHeight: parent.height / 2
                }
            }
        }

        onAcctNoChanged: {
            custDetail2 = g_MainQuo.getCustDetail(acctNo)
            custDe.custDetail = custDetail2
            var nPosInfoSize = custDetail2.getAlmSize("alm_posi_info")
            console.log("nPosInfoSize:" + acctNo)
            for (var i = 0; i < nPosInfoSize; ++i) {
                posi_info.append({
                                     ProdCode: custDetail2.getAlmString(
                                                   "alm_posi_info", i, 0),
                                     CurrLongAmt: custDetail2.getAlmString(
                                                      "alm_posi_info", i, 1),
                                     LongAvg: custDetail2.getAlmString(
                                                  "alm_posi_info", i, 2),
                                     LongOpenAvg: custDetail2.getAlmString(
                                                      "alm_posi_info", i, 3),
                                     CurrShortAmt: custDetail2.getAlmString(
                                                       "alm_posi_info", i, 4),
                                     ShortAvg: custDetail2.getAlmString(
                                                   "alm_posi_info", i, 5),
                                     ShortOpenAvg: custDetail2.getAlmString(
                                                       "alm_posi_info", i, 6),
                                     Surplus: custDetail2.getAlmString(
                                                  "alm_posi_info", i, 7),
                                     CurrPrice: custDetail2.getAlmString(
                                                    "alm_posi_info", i, 8)
                                 })
            }

            var nStoreSize = custDetail2.getAlmSize("alm_stor_info")
            for (var k = 0; k < nStoreSize; ++k) {
                store_info.append({
                                      VarietyId: custDetail2.getAlmString(
                                                     "alm_stor_info", k, 0),
                                      CurrAmt: custDetail2.getAlmString(
                                                   "alm_stor_info", k, 1),
                                      CurrCanUse: custDetail2.getAlmString(
                                                      "alm_stor_info", k, 2),
                                      AppFroz: custDetail2.getAlmString(
                                                   "alm_stor_info", k, 3)
                                  })
            }
        }

        ListModel {
            id: posi_info
        }
        ListModel {
            id: store_info
        }
    }
}
